
/*
 * Hlpay-Plus aggregate payment system. Copyright
 * (c) 2022-2023 Hlpay Team Copyright has the right of final interpretation.
 */
package com.hlkj.pay.util;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.pay.app.merchant.ext.impl.fuyou.constant.FuYouConstant;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Bob
 * @description 入参工具类
 * @date 2024/09/14
 */
public class ParamUtils {
    private ParamUtils() {
        throw new IllegalStateException("Utility class");
    }

    public static <T> PageResult<T> buildPageResult(PageResult<?> pageResult, List<T> list) {
        PageResult<T> result = new PageResult<>();
        result.setPageNo(pageResult.getPageNo());
        result.setPageSize(pageResult.getPageSize());
        result.setPages(pageResult.getPages());
        result.setTotal(pageResult.getTotal());
        result.setList(list);
        return result;
    }

    public static <T> CommonResult<PageResult<T>> buildPageCommonResult(PageResult<?> pageResult, List<T> list) {
        PageResult<T> result = new PageResult<>();
        result.setPageNo(pageResult.getPageNo());
        result.setPageSize(pageResult.getPageSize());
        result.setPages(pageResult.getPages());
        result.setTotal(pageResult.getTotal());
        result.setList(list);
        return CommonResult.success(result);
    }
    public static Map<String, Object> xmlToObject(String xmlStr,String charset) {
        String decodedXML;
        if (xmlStr.startsWith("<?xml")) {
            decodedXML = xmlStr;
        }
        else {
            try {
                decodedXML = URLDecoder.decode(xmlStr, charset); // 使用 UTF-8 编码，或根据 FuYouConstant.charset 设置
            }
            catch (Exception e) {
                return new HashMap<>();
            }
        }
        Map<String, Object> map = new HashMap<>();
        Document doc;
        try {
            doc = DocumentHelper.parseText(decodedXML); // 解析 XML 字符串为 Document
            Element root = doc.getRootElement(); // 获取根节点
            map = parseElementToMap(root); // 解析根节点及其子元素
        }
        catch (DocumentException e) {
            e.printStackTrace();
        }

        return map;
    }
    // 递归地解析每个元素及其子元素
    private static Map<String, Object> parseElementToMap(Element element) {
        Map<String, Object> elementMap = new HashMap<>();

        List<Element> children = element.elements(); // 获取当前元素的所有子元素

        // 如果有子元素，遍历它们
        if (children != null && !children.isEmpty()) {
            for (Element child : children) {
                String childName = child.getName();
                List<Element> childElements = child.elements(); // 获取该子元素下的所有子节点

                // 如果有多个相同的子元素，则处理为列表
                if (childElements != null && !childElements.isEmpty()) {
                    List<Object> childValues = new ArrayList<>();
                    for (Element e : childElements) {
                        // 递归解析嵌套的 XML
                        childValues.add(parseElementToMap(e));
                    }
                    elementMap.put(childName, childValues); // 将该节点的多个值（作为对象列表）放入 Map 中
                }
                else {
                    // 如果没有嵌套元素，直接存储它的文本值
                    elementMap.put(childName, child.getStringValue().trim());
                }
            }
        }

        return elementMap;
    }
}
